<?php
namespace App\Libs\User;
use \App\Libs\DB\PailePdo;
use \App\Libs\Exception\RuntimeException;
use \App\Libs\Base\Constant;
class User extends \App\Libs\Base\AccessObject
{
	protected static $salt = 'Just-4#Holp';
	private static function generate($identify) {
		return  md5(md5($identify . ':' . mt_rand(1, 1000)) . microtime(true));
	}
	public function regist($info) {
		$token = $this->getUniqueToken($token);

		$info['token'] = $token;
		$info['ctime'] = date('Y-m-d H:i:s');
		$info['password'] = md5($info['password'] . ':' self::$salt);

		return PailePdo::insert('t_paile_user_info', $info);
	}

	public function getUniqueToken($info) {
		// generate token
		$identify = '';
		if (!empty($info['phone']) {
			$identify = $info['phone'];
		} else if (!empty($info['wx_unionid'])) {
			$identify = $info['wx_unionid'];
		} else {
			$identify = $info['nickname'];
		}
		do {
			$token = $this->generate($identify);
			
			$isExist = $this->getUserByToken($token);

		} while($isExist);

		return $token;
	}

	public function getUserByToken($token) {
		$sql = 'SELECT * FROM t_paile_user_info WHERE token=?';
		$res = PailePdo::find($sql, array($token));
		return self::instance($res);
	}

	public function getUser($params) {
		foreach 
	}
    
    public static function getUserInfoById($id){
        $sql = "SELECT id,username,tel,head_url,credential_type,credential_no,email,unionid,score FROM  t_paile_user_info  WHERE  id = ?  ";
        $userinfo = PailePdo::find($sql,  array($id) );
        return $userinfo ;
    }


     public static function getUserInfoByUnionid($unionid){
        $sql = "SELECT id,username,tel,head_url,credential_type,credential_no,email,unionid,score FROM  t_paile_user_info  WHERE  unionid = ?  ";
        $userinfo = PailePdo::find($sql,  array($unionid) );
        return $userinfo ;
    }


     public static function getUserInfoBytel($tel){
        $sql = "SELECT id,username,tel,head_url,credential_type,credential_no,email,unionid,score FROM  t_paile_user_info  WHERE  tel = ?  ";
        $userinfo = PailePdo::find($sql,  array($tel) );
        return $userinfo ;
    }

    public static function getAccessTokenById($id){
        $sql = "SELECT * FROM  t_paile_session  WHERE  uid = ?  ";
        $key = PailePdo::find($sql,  array($id) );
        return $key['key'] ;
    }
}
